package com.simen.hellobytes.lang;

import com.simen.hellobytes.lang.interfaces.MapTraverse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * author: SimenHi
 * create time: 2020-05-18 15:29
 * description:
 */
public class MapUtil {

    public static <K, V> void iterator(Map<K, V> map, MapTraverse<K, V> traverse) {
        Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<K, V> entry = iterator.next();

            traverse.traverse(entry.getKey(), entry.getValue());
        }
    }

    public static <K, V> Map<K, V> create(K[] ks, V[] vs) {
        Map<K, V> map = new HashMap<K, V>();

        for (int i = 0; i < ks.length; i++) {
            map.put(ks[i], vs[i]);
        }

        return map;
    }

    public static <K, V> Map<K, V> create(K k, V v) {
        Map<K, V> map = new HashMap<K, V>();
        map.put(k, v);
        return map;
    }

    public static Map<String, String> create(String... kvs) {
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i + 1 < kvs.length; i += 2) {
            map.put(kvs[i], kvs[i + 1]);
        }
        return map;
    }

}
